package com.tuyan.aixiang.component;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.lang.Consumer;
import com.tuyan.aixiang.common.lang.Consumer2;
import com.tuyan.aixiang.model.YsDevice;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

public class VidOperateDialog extends Dialog {

    private YsDevice device;

    private VidItem vidItem;

    private Consumer<VidItem> onRefreshConsumer;

    private Consumer<YsDevice> onSettingConsumer;

    private Consumer<YsDevice> onReplayConsumer;

    private SwitchCompat scDetect;

    public VidOperateDialog(@NonNull Context context) {
        super(context, R.style.OpDialog);
    }

    public VidOperateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected VidOperateDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vid_operate_dialog);

        setCanceledOnTouchOutside(false);

        LinearLayout llDetect = findViewById(R.id.ll_detect);
        LinearLayout llRefresh = findViewById(R.id.ll_refresh);
        LinearLayout llReplay = findViewById(R.id.ll_replay);
        LinearLayout llSetting = findViewById(R.id.ll_setting);

        scDetect = findViewById(R.id.sc_detect);

        LinearLayout llCancel = findViewById(R.id.ll_cancel);
        llCancel.setOnClickListener(view -> dismiss());
        getWindow().setGravity(Gravity.BOTTOM); //显示在底部

        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
        getWindow().setAttributes(p);

        llRefresh.setOnClickListener(v -> {
            if(null != this.onRefreshConsumer) {
                onRefreshConsumer.accept(vidItem);
            }
        });

        llSetting.setOnClickListener(v -> {
            if(null!= this.onSettingConsumer) {
                onSettingConsumer.accept(device);
            }
        });

        llReplay.setOnClickListener(v -> {
            if(null != this.onReplayConsumer) {
                onReplayConsumer.accept(device);
            }
        });

        scDetect.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (disableWatch) return;
            VidOperateDialog.this.toggleDetect(isChecked? 1: 0);
        });
    }

    Consumer2<String, Integer> cons;
    Boolean disableWatch = false;

    public void setOnToggleDetect(Consumer2<String, Integer> consumer) {
        cons = consumer;
    }

    public void toggleDetect(Integer willCode) {
        if(null != cons) {
            cons.accept(this.device.getDeviceSerial(), willCode);
        }
    }

    public void syncToggleDetect(Integer code) {
        this.device.setDefence(code);
        disableWatch = true;
        scDetect.setChecked(this.device.getDefence().equals(1));
        disableWatch = false;
    }

    public void show(YsDevice vid, VidItem vidItem) {
        this.device = vid;
        this.vidItem = vidItem;
        this.show();
    }

    @Override
    public void show() {
        super.show();
        disableWatch = true;
        scDetect.setChecked(this.device.getDefence().equals(1));
        disableWatch = false;

    }

    public void setOnRefresh(Consumer<VidItem> consumer) {
        this.onRefreshConsumer = consumer;
    }

    public void setOnSetting(Consumer<YsDevice> consumer) {
        this.onSettingConsumer = consumer;
    }

    public void setOnReplay(Consumer<YsDevice> consumer) {
        this.onReplayConsumer = consumer;
    }

}
